##

rm(list = ls())

## 

library(tidyverse)

## Load saved results

res_df_rdd_muni <- read_rds('data/rdd_municipal_results.rds') %>% 
  mutate(unit = 'Municipal election') %>% 
  filter(str_detect(outcome, 'agg|turnout'))

res_df_rdd_bundestag <- read_rds('data/rdd_federal_results.rds') %>% 
  mutate(unit = 'Federal election') %>% 
  filter(str_detect(outcome, 'agg|turnout')) %>% 
  mutate(outcome = str_remove(outcome, "_party"))

res_df_rdd_landtag <- read_rds('data/rdd_state_results.rds') %>% 
  mutate(unit = 'State election') %>% 
  filter(str_detect(outcome, 'agg|turnout'))
  
## Combine and do some cleanup / renaming

results_rdd <- list(res_df_rdd_landtag, 
                    res_df_rdd_bundestag, 
                    res_df_rdd_muni)%>% 
  reduce(bind_rows) %>%
  filter(!str_detect(outcome, 'cand')) %>% 
  mutate(exclude_state = ifelse(exclude_state == "State excluded",
                                'B-W excluded', 'Full sample')) %>% 
  mutate(outcome= recode(outcome, 
                         `agg_left` = 'Left-wing parties',
                         `agg_center` = 'Centrist parties',
                         `agg_right` = 'Right-wing parties',
                         `turnout` = 'Turnout')) %>% 
  mutate(unit = factor(unit, levels = unique(unit)[c(4, 2,1,3)])) %>% 
  mutate(outcome = factor(outcome, levels = unique(outcome)[c(4, 1,3,2)]))

## Table with detailed results ##

turn_df <- results_rdd %>% 
  mutate_at(vars(estimate, matches('conf'), 
                 pval), list(~round(., 2))) %>% 
  mutate(ci = paste0('[',conf.low, ', ',conf.high, ']')) %>% 
  dplyr::select(outcome, exclude_state, estimate, 
                ci, pval, bw, bw_bias, n, unit) %>%
  filter(unit == 'Federal election') %>% dplyr::select(-unit) %>% 
  mutate(bw = round(bw, 0), bw_bias = round(bw_bias, 0))

## Load saved RI pvals
## These are produced the .R file the produces figure 4

rip <- read_rds('data/ri_pvals.rds') %>% 
  mutate(outcome= recode(outcome, 
                         `Left parties` = 'Left-wing parties',
                         `Center parties` = 'Centrist parties',
                         `Right parties` = 'Right-wing parties',
                         `turnout` = 'Turnout'))

## Combine

turn_df <- left_join(turn_df, rip)
turn_df <- turn_df %>% 
  dplyr::select(-bw, -bw_bias, -n, everything()) %>% 
  mutate(pval_ri = round(pval_ri, 2))

# Table A3: main results w/ details ----

kable(turn_df, "latex", longtable = F, 
      booktabs = T, col.names = c('Outcome',
                                  'Sample', 
                                  '$\\hat{\\tau}_{\\text{SRD}}$',
                                  '95\\% CI', 
                                  'P-val',
                                  'P-val (RI)',
                                  '$h_{\\text{MSE}}$',
                                  '$b_{\\text{MSE}}$',
                                  '$n$'),
      linesep = "", caption = 'Main RDD results',
      label = 'tab:main_appendix',
      escape = F) %>%
  kable_styling(latex_options = c("repeat_header"),
                font_size = 12) %>% 
  row_spec(0, bold = T) %>% 
  kable_styling(latex_options = "HOLD_position")
